import { ref } from 'vue'
import { defineStore } from 'pinia'
import { loginAPI } from '@/apis/login'
import { useCartStore } from './cart'

export const useUserStore = defineStore('user', () => {
  const cartStore = useCartStore()
  // 1.定义与用户相关的state
  const userInfo = ref({})
  // 2.定义与用户相关的action
  const getUserInfo = async({account, password}) => {
    const res = await loginAPI({
      account,
      password
    })
    userInfo.value = res.result
    cartStore.mergeCart() // 合并购物车
  }
  const clearUserInfo = () => {
    userInfo.value = {} 
    cartStore.clearCart() // 清空购物车
  }
  // 3.将state和action返回出去
  return { userInfo, getUserInfo, clearUserInfo }
},{
  persist: true // 持久化
})